package com.example.es.rest;

import com.example.es.dataobject.ProductPO;
import com.example.es.dataobject.ProductQueryForm;
import com.example.es.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author luowj12
 * @since 2021/3/22 11:14
 */
@Slf4j
@RestController
@RequestMapping("es/product")
public class EsController {
    @Autowired
    private ProductService productService;

    @PostMapping("/save")
    public void save(@RequestBody ProductPO product){
        productService.save(product);
    }

    /**
     * 这里要注意，如果使用 save 方法来更新的话，必须是全量字段，否则其它字段会被覆盖。
     */
    @PostMapping("/update")
    public void update(@RequestBody ProductPO product) {
        productService.save(product);
    }

    /**
     * 根据 ID 编号，删除一条记录
     * @param id
     */
    @RequestMapping(value="/{id}",method= RequestMethod.DELETE)
    public void delete(@PathVariable("id") Integer id) {
        productService.delete(id);
    }


    /**
     * 根据 ID 编号，查询一条记录
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    public ProductPO selectById(@RequestParam Integer id) {
        return productService.selectById(id);
    }

    /**
     * 获取所有的数据。并制定范围
     * @param idList
     * @return
     */
    @PostMapping("/selectByIdList")
    public List<ProductPO> selectByIds(@RequestBody List<Integer> idList) {
        return productService.selectByIds(idList);
    }

    @PostMapping("/listProductPO")
    public List<ProductPO> listProductPO(@RequestBody ProductQueryForm form){
        return productService.listProductPO(form);
    }

    @PostMapping("/listProductPOBy")
    public List<ProductPO> listProductPOBy(@RequestBody ProductQueryForm form){
        return productService.listProductPOBy(form);
    }
}
